<?php
/*
 * ---------------------------------------------------------------
* Author  : Anthony Tran
* Email   : Incredibletran@gmail.com - Incredibletran@hotmail.com
* Version : 1.2
* ---------------------------------------------------------------
*/

class Group_model extends CI_Model{

	var $table			=	"gallery_cate";
	var $table_image	=	"gallery";
	public function __construct(){
		parent::__construct();
	}
	
	
	function get_all(){
		$res	=	$this->db->select()
							 ->get($this->table)
							 ->result();
		if(!empty($res)){
			return $res;
		}
		return false;
	}
	
	
	function get_list($perpage = "all", $offset = 0){
		$this->db->select();
		if($perpage != "all"){
			$this->db->limit($perpage, $offset);
		}
		$this->db->order_by('id', 'DESC');
		$query	=	$this->db->get($this->table);
		$result	=	$query->result();
		if(!empty($result)){
			return $result;
		}
		return false;
	}
	
	
	function add($info	=	array()){
		$res	=	$this->db->insert($this->table, $info);
		return $res;
	}
	
	function edit($id = 0, $info = array()){
		$this->db->where('id', $id)->update($this->table, $info);
	}
	
	function update_order($id, $info = array()){
		return $this->db->where('id', $id)->update($this->table, $info);
	}
	
	
	function delete($id){
		return $this->db->where('id', $id)->delete($this->table);
	}
	
	
	function count_all(){
		return $this->db->select()->count_all($this->table);
	}
	
	
	function check_exist($name = ""){
		$res	=	$this->db->select()
							->where('name', $name)
							->get($this->table)
							->row_array();
		if(!empty($res)){
			return true;
		}
		return false;
	}
	
	function check_order_exist($order = 0){
		$res	=	$this->db->select()
							->where('order', $order)
							->get($this->table)
							->row_array();
		if(!empty($res)){
			return true;
		}
		return false;
	}
	
	
	function check_exist_edit($id, $name){
		$res	=	$this->db->select()
							->where('id !=', $id)
							->where('name', $name)
							->get($this->table)
							->row_array();
		if(!empty($res)){
			return true;
		}
		return false;
	}
	
	function check_order_exist_edit($id, $order){
		$res	=	$this->db->select()
							->where('id !=', $id)
							->where('order', $order)
							->get($this->table)
							->row_array();
		if(!empty($res)){
			return true;
		}
		return false;
	}
	
	
	function get_match($id = 0){
		$res	=	$this->db->select()
							->where('id', $id)
							->get($this->table)
							->row();
		if(!empty($res)){
			return $res;
		}
		return false;
	}
	
	function get_match_order($order){
		$res	=	$this->db->where('order', $order)
							->get($this->table)
							->row_array();
		if(count($res)>0){
			return $res;
		}
		return $res;
	}
	
	function get_images($id){
		$res	=	$this->db->select()
							 ->where('cate_id', $id)
							 ->get($this->table_image)
							 ->result();
		if(!empty($res)){
			return $res;
		}
		return false;
	}
	
	function delete_images($id){
		$this->db->where('cate_id', $id)->delete($this->table_image);
	}

	
}

/*---------------------------------------------End-----------------------------------------*/